package github.flandre.examplemod.common.container;

import github.flandre.examplemod.common.tileentity.ExampleGuiTileEntity;
import github.flandre.examplemod.core.init.BlockInit;
import github.flandre.examplemod.core.init.ContainerInit;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.IIntArray;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.IntArray;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

import javax.annotation.Nullable;
import java.util.Objects;

/*
 * ExampleGuiBlock 机器方块本身
 * ExampleGuiTileEntity 机器的实体
 * ExampleGuiContainer 实体的界面
 * DisplayCaseScreen 客户端渲染贴图
 * TileEntityInit 初始化
 * ContainerInit 初始化
 * */

public class ExampleGuiContainer extends Container {
    //绑定到界面的方块实体
    public final ExampleGuiTileEntity tileEntity;

    private final IWorldPosCallable abelToInteract; //不知道干嘛的

    //机器槽位
    private final LazyOptional<IItemHandler> UP;
    private final LazyOptional<IItemHandler> DOWN;
    private final LazyOptional<IItemHandler> SIDE;

    public final IIntArray data;

    //这个是真正处理的方法 data处理进度条的
    public ExampleGuiContainer(final int id,
                               final PlayerInventory playerInventory,
                               final ExampleGuiTileEntity entity,
                               IIntArray data) {
        super(ContainerInit.EXAMPLE_GUI_CONTAINER.get(), id);
        this.tileEntity = entity;
        this.abelToInteract = IWorldPosCallable.of(tileEntity.getWorld(),tileEntity.getPos());

        this.data = data;
        this.trackIntArray(data);//建立关系

        Capability<IItemHandler> IHCap = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;

        this.UP = tileEntity.getCapability(IHCap, Direction.UP);
        this.DOWN = tileEntity.getCapability(IHCap, Direction.DOWN);
        this.SIDE = tileEntity.getCapability(IHCap, Direction.NORTH);

        this.UP.ifPresent((s)->{
            //80 32 槽的位置
            this.addSlot(new SlotItemHandler(s,0,80,32));
        });
        this.DOWN.ifPresent((s)->{
            //80 32 槽的位置
            this.addSlot(new SlotItemHandler(s,0,134,59));
        });
        this.SIDE.ifPresent((s)->{
            //80 32 槽的位置
            this.addSlot(new SlotItemHandler(s,0,26,59));
        });


        //创建玩家背包
        for(int i = 0 ; i < 9 ; i++)
        {
            this.addSlot(new Slot(playerInventory,i,i*18+8,152));
            this.addSlot(new Slot(playerInventory,i+9,i*18+8,94));
            this.addSlot(new Slot(playerInventory,i+18,i*18+8,112));
            this.addSlot(new Slot(playerInventory,i+27,i*18+8,130));
        }


    }

    //id 物品栏 包 这个方法是为了注册用的
    public ExampleGuiContainer(final int id,
                               final PlayerInventory playerInventory,
                               final PacketBuffer data) {
        this(id,playerInventory,getTileEntity(playerInventory,data),new IntArray(1));
    }

    public static ExampleGuiTileEntity getTileEntity(PlayerInventory playerInventory,
                                                     PacketBuffer data)
    {
        Objects.requireNonNull(playerInventory,"玩家物品栏不为空");
        Objects.requireNonNull(data,"数据包不能为空");
        //获取方块位置
        BlockPos blockPos = data.readBlockPos();
        TileEntity tileEntity1 = playerInventory.player.world.getTileEntity(blockPos);
        if(tileEntity1 instanceof ExampleGuiTileEntity)
        {
            return (ExampleGuiTileEntity)tileEntity1;
        }
        throw new IllegalStateException("不合法的方块实体");
    }

    //是否可以打开物品栏
    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        //判断方块距离玩家距离是否可以打开
        //isWithinUsableDistance(abelToInteract,playerIn, BlockInit.GUI_BLOCK.get())

        return true;
    }

    //按下shift后把物品丢到槽位上在这里实现
    @Override
    public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
        return ItemStack.EMPTY;
    }
}
